/**
 * @Title: SuggestionsController.java
 * @Package me.springboot.mybatis.web
 * @Description: TODO
 * @author zengh
 * @date 2018/04/29
 * @version V1.0
 */
package me.springboot.mybatis.web;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import me.springboot.mybatis.common.utils.SessionUtil;
import me.springboot.mybatis.common.utils.StringUtil;
import me.springboot.mybatis.core.RetResponse;
import me.springboot.mybatis.core.RetResult;
import me.springboot.mybatis.exception.ResultException;
import me.springboot.mybatis.model.Suggestions;
import me.springboot.mybatis.model.User;
import me.springboot.mybatis.service.SuggestionsService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @author zengh
 * @ClassName: SuggestionsController
 * @Description: TODO
 * @date 2018/04/29
 */
@RestController
@RequestMapping("/suggestions")
@CrossOrigin
public class SuggestionsController {

    @Resource
    private SuggestionsService suggestionsService;

    /**
     * @param suggestions
     * @Title: add
     * @Description: 添加意见反馈
     * @Reutrn RetResult<Object>
     */
    @PostMapping("/add")
    public RetResult<Object> add(HttpServletRequest request, Suggestions suggestions) throws ResultException {
        if (StringUtil.isBlank(suggestions.getUserid())) {
            User user = SessionUtil.getSessionUser(request);
            suggestions.setUserid(user.getuId());
        }
        suggestionsService.save(suggestions);
        return RetResponse.makeOKRsp();
    }

    /**
     * @param suggestions
     * @Title: add2
     * @Description: 传入参数为JSON格式
     * @Reutrn RetResult<Object>
     */
    @PostMapping("/add2")
    public RetResult<Object> add2(@RequestBody Suggestions suggestions) {
        suggestionsService.save(suggestions);
        return RetResponse.makeOKRsp();
    }

    /**
     * @param suggestions
     * @Title: delete
     * @Description: 删除
     * @Reutrn RetResult<Object>
     */
    @PostMapping("/delete")
    public RetResult<Object> delete(@RequestParam Integer id) {
        suggestionsService.deleteById(id);
        return RetResponse.makeOKRsp();
    }

    /**
     * @param suggestions
     * @Title: update
     * @Description: 更新
     * @Reutrn RetResult<Object>
     */
    @PostMapping("/update")
    public RetResult<Object> update(Suggestions suggestions) {
        suggestionsService.update(suggestions);
        return RetResponse.makeOKRsp();
    }

    /**
     * @param suggestions
     * @Title: update
     * @Description: 更新(传入参数为JSON格式)
     * @Reutrn RetResult<Object>
     */
    @PostMapping("/update2")
    public RetResult<Object> update2(@RequestBody Suggestions suggestions) {
        suggestionsService.update(suggestions);
        return RetResponse.makeOKRsp();
    }

    /**
     * @param suggestions
     * @Title: detail
     * @Description: 根据ID查询详情
     * @Reutrn RetResult<Suggestions>
     */
    @PostMapping("/detail")
    public RetResult<Suggestions> detail(@RequestParam Integer id) {
        Suggestions suggestions = suggestionsService.findById(id);
        return RetResponse.makeOKRsp(suggestions);
    }

    /**
     * @param reqMap
     * @Title: detail
     * @Description: 传入参数为JSON格式
     * @Reutrn RetResult<Suggestions>
     */
    @PostMapping("/detail2")
    public RetResult<Suggestions> detail(@RequestBody Map<String, Object> reqMap) {
        Integer id = Integer.valueOf(reqMap.get("id").toString());
        Suggestions suggestions = suggestionsService.findById(id);
        return RetResponse.makeOKRsp(suggestions);
    }

    /**
     * @param page 页码
     * @param size 每页条数
     * @Title: list
     * @Description: 分页查询
     * @Reutrn RetResult<PageInfo<Suggestions>>
     */
    @PostMapping("/list")
    public RetResult<PageInfo<Suggestions>> list(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "0") Integer size) {
        PageHelper.startPage(page, size);
        List<Suggestions> list = suggestionsService.findAll();
        PageInfo<Suggestions> pageInfo = new PageInfo<Suggestions>(list);
        return RetResponse.makeOKRsp(pageInfo);
    }
}